<?php

class Gen {

    /**
     * 间于最小数和最大数之间的的随机数字
     *
     * @param float $min 最小数
     * @param float $max 最大数
     * @return float 
     */
    public static function rand(float $min, float $max) {
        return mt_rand($min, $max);
    }

    /**
     * 按位数生成字符型随机字符串
     *
     * @param integer $length 生成的随机字符串长度，默认4
     * @return string 
     */
    public static function char(int $length = 4) {
        return self::_random('char', $length);
    }

    /**
     * 按位数生成数字型随机字符串
     *
     * @param integer $length 生成的随机字符串长度，默认4
     * @return string 
     */
    public static function number(int $length = 4) {
        return self::_random('number', $length);
    }

    /**
     * 按位数生成混合型随机字符串
     *
     * @param integer $length 生成的随机字符串长度，默认8
     * @return string 
     */
    public static function mixed(int $length = 8) {
        return self::_random('mixed', $length);
    }

    /**
     * 批量生成各类型的随机字符串数组
     *
     * @param string $type 生成的随机字符串类型（可取值：char, number, mixed），默认mixed
     * @param integer $length 生成的随机字符串长度
     * @param integer $count 生成的数量
     * @return array 
     */
    public static function batch(string $type, int $length, int $count) {

        // 如果type的值不正确，则默认使用mixed
        if ($type != 'char' && $type != 'number' && $type != 'mixed') {
            $type = 'mixed';
        }

        // 生成数组并返回
        $list = [];
        self::_batch($type, $length, $count, $list);
        return $list;
    }

    /**
     * _batch
     */
    private static function _batch(string $type, int $length, int $count, &$list) {
        $current = count($list);
        for ($i = 0; $i < $count - $current; $i++) {
            $list[] = self::_random($type, $length);
        }
        array_unique($list);
        if (count($list) < $count) {
            self::batch($type, $length, $count, $list);
        }
    }

    /**
     * _random
     */
    private static function _random(string $type, int $length = 8) {
        $char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $number = '0123456789';
        $mixed = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
        $source = $$type;
        $result = '';
        for ($i = 0; $i < $length; $i++) {
            $result .= substr($source, mt_rand(0, strlen($source) - 1), 1);
        }
        return $result;
    }
}